package com.elitetyc.eliteim.server.handler.impl;

import cn.hutool.core.util.TypeUtil;
import com.elitetyc.eliteim.common.constant.Constants;
import com.elitetyc.eliteim.common.handler.impl.BaseMsgHandler;
import com.elitetyc.eliteim.common.model.IMUserInfo;
import com.elitetyc.eliteim.common.model.impl.UserLoginData;
import com.elitetyc.eliteim.common.proto.RequestProto;
import com.elitetyc.eliteim.server.holder.SessionSocketHolder;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Map;

/**
 * @author yuanchao.tan
 * @version 1.0
 * @description 登陆消息处理器
 * @Date 2021/9/16 13:37
 */
@Component
@Slf4j
public class LoginMsgHandlerImpl extends BaseMsgHandler<UserLoginData> {

    @Override
    public boolean support(int type) {
        return Constants.RequestType.LOGIN==type;
    }

    @Override
    public void handleMsg(ChannelHandlerContext ctx, RequestProto.RequestProtocol source, UserLoginData msg) {
        // 登陆的时候可能是换账号登陆，这里channel复用，先移除原来的
        SessionSocketHolder.removeSession((NioSocketChannel) ctx.channel());
        // 添加新的用户信息
        SessionSocketHolder.putSession(msg.getUserId(), new SessionSocketHolder.ServerChannelUserInfo(
                msg, (NioSocketChannel) ctx.channel()
        ));
        log.info("user {} with id {} login in server success",msg.getUserName(),msg.getUserId());
    }


}

